package com.example.demoweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demodao.entity.User;
import com.example.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: yooli
 * @description: 用户类控制层
 * @author: qiang.ye
 * @create: 2019-04-10
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private RedisTemplate<String, User> redisTemplate;
    @Autowired
    private IUserService iUserService;


    @RequestMapping("/saveUser")
    public String saveUser(String name,Integer age){

        User user = new User(name,age);
        List<User> userList = null;
        try {

            iUserService.saveUser(user);

            redisTemplate.opsForValue().set(name,user);
            userList = iUserService.findAll();
        }catch (Exception e){
            e.printStackTrace();
        }

        return null == userList?"":JSONObject.toJSONString(userList);
    }


    @RequestMapping(value = "/getKey")
    public String getKey(String key){
        User user = redisTemplate.opsForValue().get(key);
        return null == user?"":JSONObject.toJSONString(user);
    }


}
